package org.edu.modules.stu.jis.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.edu.common.dto.ExcelPo;
import org.edu.common.utils.PoiUtils;
import org.edu.modules.stu.base.entity.VeBaseStudent;
import org.edu.modules.stu.base.service.VeBaseStudentInfoService;
import org.edu.modules.stu.base.service.VeBaseStudentService;
import org.edu.modules.stu.common.service.VeBaseBanjiService;
import org.edu.modules.stu.jis.dto.VeBaseBanjiBzrDto;
import org.edu.common.dto.AllDto;
import org.edu.modules.stu.jis.entity.VeBaseBanjiBzr;
import org.edu.modules.stu.jis.service.VeBaseBanjiBzrService;
import org.edu.common.api.vo.Result;
import org.edu.common.base.PageDto;
import org.edu.common.dto.StatusDto;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = {"班主任表"})
@RestController
@RequestMapping("/stu/veBaseBanjiBzr")
@Slf4j
public class VeBaseBanjiBzrController {

    @Autowired
    private VeBaseBanjiBzrService veBaseBanjiBzrService;
    @Autowired
    private VeBaseStudentService veBaseStudentService;

    @ApiOperation("分页查询班主任表")
    @PostMapping("/page")
    public Result<?> page(@RequestBody PageDto<VeBaseBanjiBzr> page) {
        QueryWrapper<VeBaseBanjiBzr> queryWrapper = page.buildQueryWrapper(VeBaseBanjiBzr.class);
        IPage<VeBaseBanjiBzrDto> pageDto = veBaseBanjiBzrService.pageDto(page, queryWrapper);
        if (pageDto.getRecords()!=null && pageDto.getRecords().size()>0) {
            for (VeBaseBanjiBzrDto dto : pageDto.getRecords()) {
                LambdaQueryWrapper<VeBaseStudent> veBaseStudentLambdaQueryWrapper = new LambdaQueryWrapper<>();
                veBaseStudentLambdaQueryWrapper.eq(VeBaseStudent::getBjId, dto.getBjId());
                veBaseStudentLambdaQueryWrapper.eq(VeBaseStudent::getXbm,1);
                List<VeBaseStudent> nansList = veBaseStudentService.listByWrapper(veBaseStudentLambdaQueryWrapper);
                dto.setNansrs(nansList.size()+"");
                veBaseStudentLambdaQueryWrapper.clear();
                veBaseStudentLambdaQueryWrapper.eq(VeBaseStudent::getBjId,dto.getBjId());
                veBaseStudentLambdaQueryWrapper.eq(VeBaseStudent::getXbm,2);
                List<VeBaseStudent> nvsList = veBaseStudentService.listByWrapper(veBaseStudentLambdaQueryWrapper);
                dto.setNvsrs(nvsList.size()+"");
            }
        }
        return Result.OK(pageDto);
    }

    /**
     * 查询所有
     */
    @ApiOperation("查询所有")
    @PostMapping("/all")
    public Result<?> all(@RequestBody AllDto<VeBaseBanjiBzr> page) {
        QueryWrapper<VeBaseBanjiBzr> queryWrapper = page.buildQueryWrapper(VeBaseBanjiBzr.class);
        List<VeBaseBanjiBzr> list = veBaseBanjiBzrService.listByWrapper(queryWrapper);
        return Result.OK(list);
    }

    @ApiOperation("导出数据）")
    @PostMapping("/export")
    public Result<?> export(@RequestBody PageDto<VeBaseBanjiBzr> page, HttpServletResponse resp) {
        QueryWrapper<VeBaseBanjiBzr> queryWrapper = page.buildQueryWrapper(VeBaseBanjiBzr.class);
        List<ExcelPo> list = new ArrayList<>();
        list.add(new ExcelPo("班级id", "bjid"));
        list.add(new ExcelPo("班主任userId", "bzruserid"));
        list.add(new ExcelPo("班主任姓名", "bzrusername"));
        list.add(new ExcelPo("设置时间", "createtime"));
        list.add(new ExcelPo("终端ID", "terminalid"));
        list.add(new ExcelPo("是否当前", "iscurrent"));
        list.add(new ExcelPo("创建用户", "createuserid"));
        list.add(new ExcelPo("", "startdate"));
        list.add(new ExcelPo("", "enddate"));
        PoiUtils.downloadFile(resp, veBaseBanjiBzrService.page(page, queryWrapper).getRecords(), list);
        return Result.OK();
    }

    @AutoLog(value = "班主任表-分页列表查询")
    @ApiOperation(value = "班主任表-分页列表查询", notes = "班主任表-分页列表查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeBaseBanjiBzr entity,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeBaseBanjiBzr> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        PageDto<VeBaseBanjiBzr> page = new PageDto<VeBaseBanjiBzr>(pageNo, pageSize);
        IPage<VeBaseBanjiBzr> pageList = veBaseBanjiBzrService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "通过id查询")
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeBaseBanjiBzr dto = veBaseBanjiBzrService.findDtoById(id);
        if (dto == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(dto);
    }

    @AutoLog(value = "添加")
    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeBaseBanjiBzrDto dto) {
        veBaseBanjiBzrService.insertOrUpdate(dto, true);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "编辑")
    @ApiOperation(value = "编辑", notes = "编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeBaseBanjiBzrDto dto) {
        veBaseBanjiBzrService.insertOrUpdate(dto, false);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "批量删除")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        veBaseBanjiBzrService.delete(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @AutoLog(value = "更新状态")
    @ApiOperation(value = "更新状态")
    @PostMapping(value = "/updateStatus")
    public Result<?> updateStatus(@RequestBody StatusDto statusDto) {
        LambdaUpdateWrapper<VeBaseBanjiBzr> updateWrapper = new LambdaUpdateWrapper<>();
        if (statusDto.getIds() != null && statusDto.getIds().size() > 0) {
            //     updateWrapper.set(VeBaseBanjiBzr::getStatus,statusDto.getStatus());
            updateWrapper.in(VeBaseBanjiBzr::getId, statusDto.getIds());
            veBaseBanjiBzrService.update(updateWrapper);
            return Result.OK("批量更新成功!");
        } else {
            return Result.error("未选择!");
        }
    }

}
